/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("ContentDynamicHtmlDetail", function (ExerciseContentResource, LanguageService, UploadFileService, $$CambioConfig, ContentDynamicHtmlResource, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('内容课程详细'));
        $scope.typeOptions = Enums.DynamicHtmlTypeEnum.options;
        $scope.contentId = $stateParams.contentId;
        $scope.loadDynamicHtml();
    });

    // ------------------------------------------------------------------------------------------------------

    $scope.loadDynamicHtml = function () {
        // summer.init('audioContent');
        ExerciseContentResource.findById({id: $scope.contentId}, {}, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.currentContentDynamicHtml = queryResult.data;

            // if($scope.currentContentDynamicHtml.studyClassType == 'AUDIO'){
            //     summer.setValue('audioContent',$scope.currentContentDynamicHtml.audioContent);
            // }
            //
            $scope.currentContentDynamicHtml.typeOption = $scope.typeOptions[$scope.currentContentDynamicHtml.studyClassType];
        });
    }

    $scope.save = function () {
        Alert.confirm(function () {
            if ($scope.currentContentDynamicHtml.studyClassType == 'AUDIO') {
                ExerciseContentResource.updateAudio({}, $scope.currentContentDynamicHtml, function (result) {
                    Alert.success();
                    $scope.loadDynamicHtml();
                });
            } else {
                ExerciseContentResource.updateDynamicHtml({}, $scope.currentContentDynamicHtml, function (result) {
                    Alert.success();
                    $scope.loadDynamicHtml();
                });
            }
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.changeDynamicHtmlType = function () {
        $scope.currentContentDynamicHtml.studyClassType = $scope.currentContentDynamicHtml.typeOption.key;

    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
    $scope.uploadPicture = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.currentContentDynamicHtml.picture = o.id;
            });
        }
    };

    $scope.uploadZip = function ($files) {
        $scope.currentContentDynamicHtml.type = 'H5';
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'FILE', $scope).then(function (o) {
                $scope.currentContentDynamicHtml.zip = o.id;
                $scope.currentContentDynamicHtml.zipName = o.originalName;
                $scope.currentContentDynamicHtml.studyClassType = $scope.toStudyClassType(o.originalName);
            });
        }
    };

    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            $scope.imageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showImageProgress, $scope.imageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.image = o.id;
            });
        }
    };

    $scope.uploadContentImage = function ($files) {
        if ($files && $files[0]) {
            $scope.contentImageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showContentImageProgress, $scope.contentImageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.contentImage = o.id;
            });
        }
    };

    $scope.download = function (fileId) {
        if (!StringUtils.isNlOrUndOrEmpty(fileId)) {
            window.open($$CambioConfig.DOWNLOAD_URL.format({id: fileId}), '_blank');
        }
    }

    $scope.uploadBackgroundImage = function ($files) {
        if ($files && $files[0]) {
            $scope.backgroundImageProgress = {};
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope, false, $scope.showBackgroundImageProgress, $scope.backgroundImageProgress).then(function (o) {
                $scope.currentContentDynamicHtml.backgroundImage = o.id;
            });
        }
    };

    var getSuffix = function (fileName) {
        var beginIndex = fileName.lastIndexOf('.');
        return fileName.substr(beginIndex + 1);
    }

    $scope.toStudyClassType = function (fileName) {
        var suffix = getSuffix(fileName).toLowerCase();
        switch (suffix) {
            case 'pdf':
                return Enums.StudyClassTypeEnum.PDF;
            case 'zip':
                return Enums.StudyClassTypeEnum.H5;
            case 'mp3':
                return Enums.StudyClassTypeEnum.AUDIO;
            case 'mp4':
                return Enums.StudyClassTypeEnum.VIDEO;
        }
        return null;
    }
});